<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;
use think\Db;
use think\Page;

class HelpTask extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'help_task';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'audittime_text',
        'distributiontime_text',
        'v_completiontime_text',
        'u_completiontime_text',
        'gradetime_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6')];
    }


    public function getAudittimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['audittime']) ? $data['audittime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDistributiontimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['distributiontime']) ? $data['distributiontime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getVCompletiontimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['v_completiontime']) ? $data['v_completiontime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUCompletiontimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['u_completiontime']) ? $data['u_completiontime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getGradetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gradetime']) ? $data['gradetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setAudittimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDistributiontimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setVCompletiontimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUCompletiontimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setGradetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function helpcategory()
    {
        return $this->belongsTo('HelpCategory', 'cat_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function helpname()
    {
        return $this->belongsTo('HelpName', 'help_name_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    /*
    *獲取互助列表
    */
    public function getHelpList($where,$field='*',$order='h.id desc')
    {
        $total = self::alias('h')
            ->where($where)
            ->join('user u','u.id = h.user_id','left')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->count();

        $page = new Page($total,10);

        $list = self::alias('h')
            ->field($field)
            ->join('user u','u.id = h.user_id')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->where($where)
            ->limit($page->firstRow.','.$page->listRows)
            ->order('h.id desc')
            ->select();

        return ['total'=>$total,'listrows'=>$page->listRows,'totalpages'=>$page->totalPages,'data'=>$list];
    }

    /*
    *獲取互助详情
    */
    public function getInfo($where,$field='*') {
        $info = self::alias('h')
            ->where($where)
            ->field($field)
            ->join('user u','u.id = h.user_id','left')
            ->join('volunteer v','v.id = h.volunteer_id','left')
            ->join('user uv','uv.id = v.user_id','left')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->find();
        if ( $info ) {
            $info['images'] = db('upload_images')->where(['type'=>'3','relation_id'=>$info['id']])->column('images');
            $info['v_avatar'] = $info['v_avatar']?$info['v_avatar']:'';
            $info['v_name'] = $info['v_name']?$info['v_name']:'';
            $info['v_mobile'] = $info['v_mobile']?$info['v_mobile']:'';
            $info['audreason'] = $info['audreason']?$info['audreason']:'';
            $info = $info->toArray();
        }
        return $info;
    }

    /*
    *详情
    */
    public function getInfos($where,$field='*') {
        $data = [];
        $info = self::where($where)->field($field)->find();
        if ( $info ) {
            $data = $info->toArray();
        }
        return $data;
    }
}
